#include "precompiled.h"
#include "Common.h"
#include "Model.h"
#include <iostream>

// Initialize our static member.
unsigned int Model::model_id_counter=0;

Model::Model()
	:_key( CreateModelID() )
{

}
Model::Model(std::string key)
	:_key(key)
{
}

Model::~Model(void)
{
}

GLuint* Model::vao(){
	return _vao;
}
GLuint Model::numIndices(){
	return _numIndices;
}


glm::vec3 Model::Get_Min(){
	return _min;
}
glm::vec3 Model::Get_Max(){
	return _max;
}

void Model::Set_vao(GLuint& pVao){
	_vao[0] = pVao;
	std::cout << "Set the model vao to: " << _vao[0] << std::endl;
}
void Model::Set_numIndices(GLuint num){
	_numIndices=num;
}

void Model::Set_Vertices(std::vector<Vertex_PNT> &vertices){
	_pvertices = new std::vector<Vertex_PNT>( vertices );
	find_extents();
}
void Model::Add_IndexBuffer(GLuint* index_buffer){
	_index_buffers.push_back(index_buffer);
}

void Model::Delete_Vertices(void){
	_pvertices->clear();
}

void Model::Delete_IndexBuffer(void){}

std::string Model::CreateModelID(){
	return "model_" + (model_id_counter++);
}

void Model::find_extents(){
	glm::vec3 tmin(FLOAT_MAX), tmax(FLOAT_MIN);

	for( int c = 0; c < _pvertices->size(); c++ ){

		tmin.x = THEMIN( tmin.x, _pvertices->at(c).x );
		tmax.x = THEMAX( tmax.x, _pvertices->at(c).x );

		tmin.y = THEMIN( tmin.y, _pvertices->at(c).y );
		tmax.y = THEMAX( tmax.y, _pvertices->at(c).y );

		tmin.z = THEMIN( tmin.z, _pvertices->at(c).z );
		tmax.z = THEMAX( tmax.z, _pvertices->at(c).z );
	}

	_min = tmin;
	_max = tmax;
}